package ch01.sec05;

import java.time.ZoneId;
import java.util.Arrays;

public class StringDemo {
    public static void main(String[] args) {
        String location = "Java";
        String greeting = "Hello " + location;
        System.out.println(greeting);
        int age = 42;
        String output = age + " years";
        System.out.println(output);

        System.out.println("Next year, you will be " + age + 1); // Error
        System.out.println("Next year, you will be " + (age + 1)); // Ok

        String names = String.join(", ", "Peter", "Paul", "Mary");
        System.out.println(names);

        // String builder

        var builder = new StringBuilder();
        for (String id : ZoneId.getAvailableZoneIds()) {
            builder.append(id);
            builder.append(", ");
        }
        String result = builder.toString();
        System.out.println(result.substring(0, 200) + "...");
        System.out.println(result.length());
        System.out.println("====================");

        // Substring

        greeting = "Hello, World!";
        location = greeting.substring(7, 12);
        System.out.println(location);

        // Equality testing

        System.out.println(location.equals("World"));
        System.out.println(location == "World");
        System.out.println(location.equalsIgnoreCase("world"));
        // 比较两个字符串的字典序顺序，返回负数表示"word"在字典序中小于"world"，0表示相等，正数表示大于
        System.out.println("word".compareTo("world"));
        System.out.println("====================");

        // Converting between numbers and strings
        int n = 42;
        String str = Integer.toString(n, 2);
        System.out.printf("String str = %s\n", str);

        int n1 = Integer.parseInt(str);
        System.out.printf("int n1 = %d\n", n1);
        int n2 = Integer.parseInt(str, 2);
        System.out.printf("int n2 = %d\n", n2);
        double x = Double.parseDouble("3.14");
        System.out.println(x);

        System.out.println(greeting.toUpperCase());
        System.out.println(greeting); // greeting is not changed

        // Unicode
        String javatm = "Java\u2122";
        System.out.println(javatm);
        System.out.println(Arrays.toString(javatm.codePoints().toArray()));
        System.out.println(javatm.length());
        System.out.println("====================");

        String octonions = "\ud835\udd46";
        System.out.println(octonions);
        System.out.println(Arrays.toString(octonions.codePoints().toArray()));
        System.out.println(octonions.length()); // Counts code units, not Unicode code points
    }
}
